package com.quick.android.tip;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * 对于PopupWindow的基本封装
 */
public abstract class BasePopWindow extends PopupWindow {
    public Activity mContext;
    public LayoutInflater mInflater;
    public View mContentView;

    public BasePopWindow(Activity context) {
        this(context, 0, 0);
    }

    public BasePopWindow(Activity context, int width, int height) {
        this(context, width, height, 0);
    }

    public BasePopWindow(Activity context, int width, int height, int style) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        initView();
        setViewSize(width, height);
        setCommonConfig();
        setAnimation(style);
    }

    abstract void initView();

    public void setView(int resId) {
        mContentView = mInflater.inflate(resId, null, false);
        setContentView(mContentView);
    }

    public View findViewById(int id) {
        return mContentView.findViewById(id);
    }

    public void setViewSize(int width, int height) {
        if (width == 0) {
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            setWidth(width);
        }
        if (height == 0) {
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            setHeight(height);
        }
    }

    /**
     * 常规设置
     */
    private void setCommonConfig() {
        // 设置弹出窗口可点击
        setFocusable(true);
        setOutsideTouchable(true);
        //刷仙状态
        update();
        // 实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作  
        this.setBackgroundDrawable(dw);
    }

    /**
     * 设置弹出窗体动画效果
     */
    public void setAnimation(int style) {
        if (style == 0) {
            return;
        }
        setAnimationStyle(style);
    }

    @Override
    public void dismiss() {
        if (mContext != null && !mContext.isFinishing()) {
            super.dismiss();
        }
    }
}
